/*
 * 作者：Peter Xiang
 * 联系方式：565067150@qq.com
 * 文档: https://github.com/PxGame
 * 创建时间: 2019/10/28 16:36:42
 */

using ActionSystem.Core;
using ActionSystem.Core;
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace XMLib.AM
{
    /// <summary>
    /// StateListView：状态列表
    /// </summary>
    [Serializable]
    public class StateListView : IDataView
    {
        public override string title => "状态列表";
        public override bool useAre => true;

        private Vector2 scrollPos;
        private List<StateItem> display = new List<StateItem>();
        protected override void OnGUI(Rect rect)
        {
            GUILayout.BeginVertical(AEStyles.box);
            //设置初始状态
            var disp = win.GetStateNames().ToArray();
            var values = win.GetStateUID().ToArray();
            EditorGUILayout.LabelField("默认状态:");
            win.config.firstStateID = EditorGUILayout.IntPopup(win.config.firstStateID, disp, values);
            GUILayout.EndVertical();
            GUILayout.Space(4);            
            win.stateSelectIndex = EditorGUILayoutEx.DrawList(win.currentStates, win.stateSelectIndex, ref scrollPos, NewState, StateDrawer);
        }

        private void NewState(Action<StateItem> adder)
        {
            var newState = new StateItem();
            if(win.config.defualtStateType!=null)
            {
                newState.state = TypeUtility.CreateInstance(win.config.defualtStateType) as BaseState;
                adder(newState);
            }
            
        }

        private int GetMaxUID()
        {
            int maxUID = BaseState.UNKNOWN_UID;
            if (win.config.states != null)
            {
                for (int i = 0; i < win.config.states.Count; i++)
                {
                    BaseState state = win.config.states[i].state as BaseState;
                    if (maxUID < state.UID)
                    {
                        maxUID = state.UID;
                    }
                }
            }

            return maxUID;
        }

        public override object CopyData()
        {
            return win.currentState;
        }

        public override void PasteData(object data)
        {
            if (win.currentStates != null && data is StateItem configs)
            {
                (configs.state as BaseState).ResetUID();
                win.currentStates.Add(configs);
            }
        }
        public void StateDrawer<T>(int index, ref bool selected, T obj) where T : StateItem
        {
            obj.state.FrameCount = obj.state.Frames.Count;
            if ((obj.state as BaseState).UID == BaseState.UNKNOWN_UID)
            {
                (obj.state as BaseState).UID = GetMaxUID() + 1;
            }            
            if(obj.GID==StateGroup.NoGroup|| win.currentStateGroup==null||obj.GID==win.currentStateGroup.GID)
            {
                if (GUILayout.Button($"{index}", selected ? AEStyles.item_head_select : AEStyles.item_head_normal, GUILayout.ExpandHeight(true), GUILayout.Width(15)))
                {
                    GUI.FocusControl(null);
                    selected = !selected;
                }
                if (GUILayout.Button($"{(obj.state as BaseState).StateName}", selected ? AEStyles.item_body_select : AEStyles.item_body_normal, GUILayout.Height(30f), GUILayout.ExpandWidth(true)))
                {
                    GUI.FocusControl(null);
                    selected = !selected;
                }
            }
            
        }
        public override void OnUpdate()
        {
        }
    }
}